﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using TesterCRM.Auxiliary;

namespace TesterCRM
{
    class Program
    {
        static void Main (string[] args)
        {
            Console.Title = "TesterCRM";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("");
            sb.AppendLine("\t//////////////////////////////////////////////////////////////");
            sb.AppendLine("\t//                         TesterCRM                        //");
            sb.AppendLine("\t//////////////////////////////////////////////////////////////");
            sb.AppendLine("");
            Console.WriteLine(sb.ToString());

            Logger logger = new Logger();

            try
            {
                logger.WriteStep("Application starts.");
                Tester tester = new Tester();
                tester.Execute();
            }
            catch ( FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex )
            {
                Logger.WriteLine("The application terminated with an error.");
                Logger.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                Logger.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                Logger.WriteLine("Message: {0}", ex.Detail.Message);
                Logger.WriteLine("Trace: {0}", ex.Detail.TraceText);
                Logger.WriteLine("Inner Fault: {0}", null == ex.Detail.InnerFault ? 
                    "No Inner Fault" : "Has Inner Fault");
            }
            catch ( TimeoutException ex )
            {
                Logger.WriteLine("The application terminated with an error.");
                Logger.WriteLine("Message: {0}", ex.Message);
                Logger.WriteLine("Stack Trace: {0}", ex.StackTrace);
                Logger.WriteLine("Inner Fault: {0}", null == ex.InnerException.Message ? 
                    "No Inner Fault" : ex.InnerException.Message);
            }
            catch ( Exception ex )
            {
                Logger.WriteLine("The application terminated with an error.");
                Logger.WriteLine(ex.Message);

                // Display the details of the inner exception.
                if ( ex.InnerException != null )
                {
                    Logger.WriteLine(ex.InnerException.Message);

                    FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                        as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if ( fe != null )
                    {
                        Logger.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                        Console.WriteLine("\n\tCode: {0}", fe.Detail.ErrorCode);
                        Console.WriteLine("\n\tMessage: {0}", fe.Detail.Message);
                        Console.WriteLine("\n\tTrace: {0}", fe.Detail.TraceText);
                        Console.WriteLine("\n\tInner Fault: {0}", null == fe.Detail.InnerFault ? 
                            "No Inner Fault" : "Has Inner Fault");
                    }
                }
            }
            finally
            {
                logger.WriteStep("The application has ended.");
                Console.ReadLine();
            }
        }
    }
}
